package cl.utfsm.isw.mecanico.vehiculo.dao;


import java.io.Reader;
import java.util.HashMap;
import java.util.List;

import cl.utfsm.isw.mecanico.vehiculo.to.VehiculoTO;
import cl.utfsm.isw.mecanico.vehiculo.to.VehiculoTO;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**s
 * @author Cristian Arredondo
 *
 */
public class VehiculoDAO {
	
	
	private static String SQLMAP = "tablas/vehiculo/sqlMapVehiculo.xml";
	
	
	/**
	 * 
	 * @param patente
	 * @return
	 * @throws Exception
	 */
	public VehiculoTO consultarVehiculoPatente(String patente) throws Exception{
		
		System.out.println("[VehiculoDAO][consultarVehiculoPatente] en VehiculoDAO");
		VehiculoTO vehiculos = null;
		
		try {
		
			HashMap parametros = new HashMap();
			parametros.put("patente", patente);
			
			System.out.println("[VehiculoDAO][consultarVehiculoPatente] parametros: "+parametros.toString());
			String resource = SQLMAP;
			Reader reader = Resources.getResourceAsReader(resource);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			
			List resultado = sqlMap.queryForList("consultarVehiculoPatente", parametros);
			
			System.out.println("[VehiculoDAO][consultarVehiculoPatente] Salida: "+ resultado.toString());
			
			if (resultado != null) {            
	            if (resultado.size() > 0) {
	            	System.out.println("[VehiculoDAO][consultarVehiculoPatente] dentro de if");
	            	vehiculos = new VehiculoTO();
	            	System.out.println("[VehiculoDAO][consultarVehiculoPatente] despues de crear vehiculo");
	                HashMap salida = (HashMap) resultado.get(0);
	                System.out.println("[VehiculoDAO][consultarVehiculoPatente] despues de casteo");
	                vehiculos.setPatente((String)salida.get("patente"));
	                vehiculos.setYear((int ) salida.get("year"));  
	                vehiculos.setMarca((String) salida.get("marca"));
	                vehiculos.setModelo((String) salida.get("modelo"));
	                vehiculos.setChasis((String) salida.get("chasis"));
	            }
			}		
			System.out.println("[VehiculoDAO][consultarVehiculoPatente] Retorno: "+ vehiculos.toString());		
			return vehiculos;
		}catch(Exception e){
			System.out.println("[VehiculoDAO][consultarVehiculoPatente] ERROR "+e);
			return null;
		}
	}
	


	/**
	 * 
	 * @param vehiculoIngreso
	 * @return
	 * @throws Exception
	 */
	public boolean ingresarVehiculo(VehiculoTO vehiculoIngreso) throws Exception{
		
		System.out.println("[VehiculoDAO][ingresarVehiculo] en VehiculoDAO");
		try{
			HashMap parametros = new HashMap();
			parametros.put("patente", vehiculoIngreso.getPatente());
			parametros.put("marca", vehiculoIngreso.getMarca());
			parametros.put("modelo", vehiculoIngreso.getModelo());
			parametros.put("chasis", vehiculoIngreso.getChasis());
			parametros.put("year", vehiculoIngreso.getYear());
						
			
			System.out.println("[VehiculoDAO][ingresarVehiculo] parametros: "+parametros.toString());
			String resource = SQLMAP;
			Reader reader = Resources.getResourceAsReader(resource);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			
			List resultado = sqlMap.queryForList("ingresarVehiculo", parametros);
			
		}catch(Exception e){
			System.out.println("[VehiculoDAO][ingresarVehiculo] ERROR : "+e.getStackTrace());
			throw e;
		}
		
		
		System.out.println("[ingresarVehiculo] datos : "+vehiculoIngreso.toString());
		
		return true;
	}
	
	/**
	 * 
	 * @param vehiculoElimina
	 * @return
	 * @throws Exception
	 */
	public boolean eliminarVehiculo(VehiculoTO vehiculoElimina) throws Exception{
		
		System.out.println("[VehiculoDAO][eliminarVehiculo] en VehiculoDAO");
		try{
			HashMap parametros = new HashMap();
			parametros.put("patente", vehiculoElimina.getPatente());
						
			
			System.out.println("[VehiculoDAO][eliminarVehiculo] parametros: "+parametros.toString());
			String resource = SQLMAP;
			Reader reader = Resources.getResourceAsReader(resource);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			
			List resultado = sqlMap.queryForList("eliminarVehiculo", parametros);
			
		}catch(Exception e){
			System.out.println("[VehiculoDAO][eliminarVehiculo] ERROR : "+e.getStackTrace());
			throw e;
		}
		
		
		System.out.println("[eliminarVehiculo] datos : "+vehiculoElimina.toString());
		
		return true;
	}
	

	/**
	 * 
	 * @param vehiculoModificar
	 * @return
	 * @throws Exception
	 */
	public boolean modificarVehiculo(VehiculoTO vehiculoModificar) throws Exception{
		
		System.out.println("[VehiculoDAO][modificarVehiculo] en VehiculoDAO");
		try{
			HashMap parametros = new HashMap();
			parametros.put("patente", vehiculoModificar.getPatente());
			parametros.put("year", vehiculoModificar.getYear());
			parametros.put("marca", vehiculoModificar.getMarca());
			parametros.put("modelo", vehiculoModificar.getModelo());
			parametros.put("chasis", vehiculoModificar.getChasis());
			
			
			System.out.println("[VehiculoDAO][modificarVehiculo] parametros: "+parametros.toString());
			String resource = SQLMAP;
			Reader reader = Resources.getResourceAsReader(resource);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			System.out.println("[VehiculoDAO][modificarVehiculo] antes de realizar consulta.");
			List resultado = sqlMap.queryForList("modificarVehiculo", parametros);
			System.out.println("[VehiculoDAO][modificarVehiculo] resultado : "+resultado.toString());
			int respuesta = 0;
			if (resultado != null) {
	                HashMap salida = (HashMap) resultado.get(0);
	                respuesta = (Integer)salida.get("respuesta");
			}
			System.out.println("[VehiculoDAO][modificarVehiculo] respuesta: [["+respuesta+"]]");
			if (respuesta==1){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			System.out.println("[VehiculoDAO][modificarVehiculo] ERROR : "+e.getStackTrace());
			throw e;
		}		
	}
}
